//
//  ResultAlertView.swift
//  SkyApp
//
//  Created by 白翊 on 2021/12/30.
//

import UIKit

class ResultAlertView: UIView {

    let INTERVAL: CGFloat = 24
    let ALERT_HEIGHT = HEIGHT * 0.50

    class func show(img: String, desc: String) {
        let view = ResultAlertView(frame: CGRect(x: 0, y: 0, width: WIDTH, height: HEIGHT))
        AppTool.rootView()?.addSubview(view)
        
        view.show(img: img, desc: desc)
    }
    
    func show(img: String, desc: String) {
        self.show()
        
        let width = WIDTH - INTERVAL * 4
        
        let bottom_space: CGFloat = ALERT_HEIGHT * 0.4
        let font = UIFont.systemFont(ofSize: 14, weight: .light)
        let desc_height = min(ALERT_HEIGHT - bottom_space - 48, (desc as NSString).height(for: font, width: width - INTERVAL * 4))
        
        let descLabel = UILabel(string: desc, fontSize: 14, frame: CGRect(x: INTERVAL * 2, y: ALERT_HEIGHT - desc_height - bottom_space, width: width - INTERVAL * 4, height: desc_height), color: .detail, textAlignment: .center)
        descLabel.font = font
        descLabel.adjustsFontSizeToFitWidth = true
        self.headerView.addSubview(descLabel)
        
        let line = UIView(frame: CGRect(x: INTERVAL * 2, y: descLabel.top - INTERVAL, width: width - INTERVAL * 4, height: 1))
        line.backgroundColor = .line
        self.headerView.addSubview(line)
        
        let picture = UIImageView(image: img, contentMode: .scaleAspectFit, frame: CGRect(x: INTERVAL, y: line.top - INTERVAL - 64, width: width - INTERVAL * 2, height: 64))
        self.headerView.addSubview(picture)
        
        let btn_width = width * 0.25
        let btn_height: CGFloat = 36
        let btn_space = (width - btn_width) * 0.5
        let btn_top = descLabel.bottom + INTERVAL * 2
        
        let button = UIButton(title: "我知道了", titleColor: .bg, fontSize: 12, frame: CGRect(x: btn_space, y: btn_top, width: btn_width, height: btn_height)) { [weak self] _ in
            
            self?.disappear()
            
        }
        button.setCornerRadius(radius: btn_height * 0.5)
        button.setThemeGradient()
        self.headerView.addSubview(button)
        
    }
    
    private func show() {
        self.maskBGView.alpha = 0
        self.headerView.snp.updateConstraints { (make) in
            make.top.equalTo(-ALERT_HEIGHT)
        }
        self.layoutIfNeeded()
        
        self.headerView.snp.updateConstraints { (make) in
            make.top.equalTo((HEIGHT - ALERT_HEIGHT) * 0.5)
        }
        
        UIView.animate(withDuration: 0.32, animations: {
            self.maskBGView.alpha = 0.72
            self.layoutIfNeeded()
        }) { (complete) in
            
        }
    }
    
    @objc private func disappear() {
        self.maskBGView.alpha = 0.72
        self.headerView.snp.updateConstraints { (make) in
            make.top.equalTo(-ALERT_HEIGHT)
        }
        
        UIView.animate(withDuration: 0.32, animations: {
            self.alpha = 0
            self.layoutIfNeeded()
        }) { (complete) in
            self.removeFromSuperview()
        }
    }
    
    lazy var headerView:UIView = {
        let view = UIView()
        view.backgroundColor = .bg
        view.layer.cornerRadius = 20
        self.insertSubview(view, aboveSubview: self.maskBGView)
        
        view.snp.makeConstraints({ (make) in
            make.left.equalTo(INTERVAL * 2)
            make.right.equalTo(-INTERVAL * 2)
            make.top.equalTo(-ALERT_HEIGHT)
            make.height.equalTo(ALERT_HEIGHT)
        })
        
        return view
    }()
    
    lazy var maskBGView:UIView = {
        let maskBGView = UIView()
        maskBGView.backgroundColor = .shadow
        self.addSubview(maskBGView)
        
        //实现模糊效果
        let effect = UIBlurEffect.init(style: UIColor.darkMode ? .dark : .light)
        let effectView = UIVisualEffectView.init(effect: effect)
        maskBGView.addSubview(effectView)
        
        maskBGView.snp.makeConstraints({ (make) in
            make.edges.equalTo(self)
        })
        
        effectView.snp.makeConstraints({ (make) in
            make.edges.equalTo(maskBGView)
        })
        
        return maskBGView
    }()


}
